package com.lj.oss.service.impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.OSSObject;
import com.lj.oss.service.OssService;
import com.lj.oss.properties.AliyunOssProperties;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.UUID;


@Service
public class OssServiceImpl implements OssService {

    @Autowired
    private AliyunOssProperties aliyunOssProperties;

    @Override
    public String upload(MultipartFile file) {
        try {
            // 创建OSSClient实例。
            OSS ossClient = new OSSClientBuilder().build(aliyunOssProperties.getEndpoint(), aliyunOssProperties.getKeyid(), aliyunOssProperties.getKeysecret());

            // 上传文件流。
            InputStream inputStream = file.getInputStream();
            //获取文件名称
            String fileOriginalFilename = file.getOriginalFilename();

            //使用uuid在文件名称里面添加唯一值
            String uuid = UUID.randomUUID().toString().replace("-", "");
            fileOriginalFilename = uuid + "_" + fileOriginalFilename;

            //这样会根据年月日创建不同的文件目录
            String datePach = new DateTime().toString("yyyy/MM/dd");

            fileOriginalFilename = datePach + "/" + fileOriginalFilename;

            //调用oss进行上传
            //第一个参数BucketName，第二个参数上传到oss文件的路径和名称,第三个文件输入流
            ossClient.putObject(aliyunOssProperties.getBucketname(), fileOriginalFilename, inputStream);

            // 关闭OSSClient。
            ossClient.shutdown();

            return "/oss/downloadFile/" + fileOriginalFilename;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public void download(String name, HttpServletResponse response) throws IOException {
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(aliyunOssProperties.getEndpoint(), aliyunOssProperties.getKeyid(), aliyunOssProperties.getKeysecret());
        OSSObject object = ossClient.getObject(aliyunOssProperties.getBucketname(), name);
        InputStream in = object.getObjectContent();

        // 读到输出流中
        response.reset();
        response.setContentType("application/octet-stream");
        response.addHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(name.split("_")[1], "UTF-8"));
        ServletOutputStream out = response.getOutputStream();
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) != -1) {
            out.write(buf, 0, len);
        }

        object.close();
        ossClient.shutdown();
    }

    @Override
    public void delete(String name) {
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(aliyunOssProperties.getEndpoint(), aliyunOssProperties.getKeyid(), aliyunOssProperties.getKeysecret());
        ossClient.deleteObject(aliyunOssProperties.getBucketname(), name);
    }

}
